<template>
  <div class="login-account">
    <el-form :model="account" :rules="rules" ref="elFormRef">
      <el-form-item label="账号" label-width="60px" prop="name">
        <el-input v-model="account.name" type="text"></el-input>
      </el-form-item>
      <el-form-item label="密码" label-width="60px" prop="password">
        <el-input v-model="account.password" type="password"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref,   } from "vue"
import { useStore } from "vuex"
import { rules } from "../config/account-config"
import { ElForm } from "element-plus"
import localCach from "@/api/localCach"
export default defineComponent({
  setup() {

    const store = useStore()

    const account = reactive({
      name: localCach.getCach("name") ?? "" ,
      password: localCach.getCach("password") ?? "" ,
    })

    const elFormRef = ref<InstanceType<typeof ElForm>>()

    const actionLogin = (isSave: boolean) => {
        elFormRef.value?.validate((valid) => {
           // 账户验证通过,开始验证是否缓存数据
          if(isSave) {
            localCach.cachLocal('name', account.name)
            localCach.cachLocal('password', account.password)
          }else {
            localCach.deleteCach('name')
            localCach.deleteCach('password')
          }
          if (!valid) return
          // 发起网络登录
          store.dispatch('login/accountLoginAction', {...account})
        })
    }

    return {
      account,
      rules,
      actionLogin,
      elFormRef,
    }
  },
})
</script>
<style lang="less" scoped>
.el-button {
  margin-left: 10px;
}
</style>
